package com.mystyle.le.weekproject.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;

import com.mystyle.le.weekproject.R;
import com.mystyle.le.weekproject.adapter.TalkFragmentAdapter;
import com.mystyle.le.weekproject.asynctaks.HeadListAsyncTaks;
import com.mystyle.le.weekproject.beans.TalkList;
import com.mystyle.le.weekproject.intentfactbackcall.MyCallBack;
import com.mystyle.le.weekproject.utils.ParseJson;
import com.mystyle.le.weekproject.utils.Url;

import java.util.ArrayList;
import java.util.List;

/**
 * 资讯界面的段子页面
 * A simple {@link Fragment} subclass.
 */
public class TalkFragment extends Fragment {


    private ListView talk_LV;
    private int index = 0;
    private List<TalkList> lists = new ArrayList<>();
    private TalkFragmentAdapter talkFragmentAdapter;
    private Button talk_add;
    private boolean isLast = false;//是否到底部

    public TalkFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_talk, container, false);
        initView(view);
        return view;
    }

    /*
    * 初始化控件
    * */
    private void initView(View view) {
        talk_LV = ((ListView) view.findViewById(R.id.talk_LV));
        talk_add = ((Button) view.findViewById(R.id.talk_add));

        talkFragmentAdapter = new TalkFragmentAdapter(lists, getActivity());
        new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
            @Override
            public void sendBack(String path) {
                List<TalkList> talkLists = ParseJson.parseTalkJson(path);//解析数据
                if (talkLists != null) {
                    lists.addAll(talkLists);
                    talk_LV.setAdapter(talkFragmentAdapter);
                }
            }
        }, getActivity()).execute(Url.getJokePadingPaht(index));

        /*
        * ListView的滑动监听
        * */
        talk_LV.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (isLast && scrollState == SCROLL_STATE_IDLE) {
                    talk_add.setVisibility(View.VISIBLE);
                } else {
                    talk_add.setVisibility(View.GONE);
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                isLast = (firstVisibleItem + visibleItemCount) == totalItemCount;
            }
        });

        /*
        * 加载更多点击事件
        * */
        talk_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index++;
                new HeadListAsyncTaks(new MyCallBack.HeadListBack() {
                    @Override
                    public void sendBack(String path) {
                        List<TalkList> talkLists = ParseJson.parseTalkJson(path);//解析数据
                        if (talkLists != null) {
                            lists.addAll(talkLists);
                            talkFragmentAdapter.notifyDataSetChanged();//刷新
                        }
                    }
                }, getActivity()).execute(Url.getJokePadingPaht(index));
            }
        });
    }

}
